package chat.server.statemachine.termin;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import chat.State;
import chat.server.statemachine.ListOfAlgorithms;
import chat.statemachine.AbstractContent;
import chat.statemachine.AlgorithmActionInterface;
import chat.statemachine.AlgorithmActionInvocationException;

/**
 * Algorithm to detect terminaison
 * @author Baptiste Lafontaine
 * @author Arsène Huot
 *
 */
public enum AlgorithmTermin implements AlgorithmActionInterface {
	TERMIN_TOKEN_MESSAGE(TerminTokenContent.class) {
		public Object execute(final State state, final AbstractContent content)
				throws AlgorithmActionInvocationException {
			return AlgorithmTerminActions.receiveTerminTokenContent(
					state, content);
		}
	};

	private final static Map<Integer, AlgorithmTermin> privateMapOfActions = new HashMap<Integer, AlgorithmTermin>();
	public final static Map<Integer, AlgorithmTermin> mapOfActions = Collections
			.unmodifiableMap(privateMapOfActions);

	public final int algorithmOffset = 900;
	private final int actionIndex;
	private final Class<? extends AbstractContent> contentClass;

	static {
		for (AlgorithmTermin aa : AlgorithmTermin
				.values()) {
			privateMapOfActions.put(aa.actionIndex, aa);
		}
	}

	private AlgorithmTermin(
			final Class<? extends AbstractContent> msgClass) {
		this.actionIndex = ListOfAlgorithms.CHAT_SERVER_START_INDEX
				+ algorithmOffset + ordinal();
		this.contentClass = msgClass;
	}

	public int getActionIndex() {
		return actionIndex;
	}

	public Class<? extends AbstractContent> getContentClass() {
		return contentClass;
	}

	public String toString() {
		return actionIndex + ", " + contentClass.getName();
	}
}